package com.example.admin.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.admin.mapper.CategoryMapper;
import com.example.admin.service.CategoryService;
import com.example.xo.pojo.Category;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author YangJiaHao
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public boolean add(Category category) {
        Category categoryInDb = findByName(category.getCategoryName());
        if (categoryInDb != null) {
            log.info("分类{}重复", category.getCategoryName());
            return false;
        }
        Snowflake snowflake = IdUtil.getSnowflake();
        String id = snowflake.nextIdStr();
        category.setCategoryId(id);
        //受影响的行数 1-成功  0-失败
        int res = categoryMapper.insert(category);

        return res == 1;
    }

    @Override
    public boolean delete(String id) {
        //todo 这里需要判断一次分类下面是否有博客，有的话就不可以删除
        Category category = findById(id);
        if(category==null){
            return false;
        }
        category.setDeleted(LocalDateTime.now());
        int res = categoryMapper.updateById(category);
        return res > 0;
    }

    @Override
    public boolean update(Category category) {
        Category categoryInDb = findByName(category.getCategoryName());
        if (categoryInDb != null) {
            log.info("分类{}重复", category.getCategoryName());
            return false;
        }
        category.setUpdated(LocalDateTime.now());
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getCategoryId,category.getCategoryId());
        int res = categoryMapper.update(category, wrapper);
        return res==1;
    }

    @Override
    public List<Category> findAll() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //后创建的排前面
        wrapper.orderByDesc(Category::getCreated);
        //软删除的不查询
        wrapper.isNull(Category::getDeleted);
        List<Category> list = categoryMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Category findById(String id) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getCategoryId,id);
        wrapper.isNull(Category::getDeleted);
        Category category = categoryMapper.selectOne(wrapper);
        return category;
    }

    @Override
    public Category findByName(String name) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getCategoryName, name);
        return categoryMapper.selectOne(wrapper);
    }
}
